package manage.action;

import m.common.action.ActionMeta;
import m.common.action.VueResult;
import m.common.model.util.FromQueryList;
import m.common.model.util.QueryOrder;
import m.html.VueCollectResult;
import m.html.VueListResult;
import m.html.element.attr.Align;
import m.html.element.attr.ButtonType;
import m.html.element.attr.TagColor;
import m.html.element.other.DisplayItem;
import m.html.list.*;
import manage.model.ServerDetailSql;
import manage.model.ServerHost;
import manage.model.ServerRequest;
import manage.model.ServerRequestDetail;

@ActionMeta(name="manage/serverHost")
public class ServerHostAction extends ManageAction {

	public VueResult serverHostList() throws Exception {
		verifyMenu("A1108");
		VueListResult result=getVueListResult("主机监控",ServerHost.class,actionUrl("serverHostList"));
		result.setSubTitle("");
		result.setHeight(-1);
		result.setFunction(true,true);
		result.addCol(toIndexColumn());
		result.addCol(toTextColumn("ipport", "ipport",true));
		result.addCol(toTextColumn("服务名称", "serverName",true));
		result.addCol(toTextColumn("服务端口", "serverPort",true));
		result.addCol(toDateTimeColumn("记录时间", "logDate",140,true).setFilter(SearchType.HIDDEN_DATE_RANGE).setSort(ListSortOrder.DESC).setCount(ListCount.MAX));
		result.addCol(toDateTimeColumn("结束时间", "endDate",140,true).setFilter(SearchType.HIDDEN_DATE_RANGE));
		result.addCol(toNumberColumn("无效请求", "invalidRequest",80,true).setSort(true).setNumberFormat("#,##0").setCount(ListCount.SUM));
		result.addCol(toNumberColumn("请求次数", "total",80,true).setSort(true).setNumberFormat("#,##0").setCount(ListCount.SUM));
		result.addCol(toNumberColumn("访问时长", "accessLong",80,true).setSort(true).setNumberFormat("#,##0.000").setCount(ListCount.SUM));
		result.addCol(toNumberColumn("平均时长", "avgLong",80,true).setSort(true).setNumberFormat("#,##0.000").setCount(ListCount.MAX));
		result.addCol(toIntegerColumn("记录SQL", "recordSql",80,true).setSort(true).setCount(ListCount.SUM));
		result.addCol(toNumberColumn("空闲", "freeMemory",76).setGroupTitle("JVM内存").setSort(true).setCount(ListCount.MAX));
		result.addCol(toNumberColumn("分配", "totalMemory",76).setGroupTitle("JVM内存").setSort(true).setCount(ListCount.MAX));
		result.addCol(toNumberColumn("最大", "maxMemory",76).setGroupTitle("JVM内存").setSort(true).setCount(ListCount.MAX));
		result.addCol(toIntegerColumn("已用", "dbUseLinkNum",76).setGroupTitle("DB链接").setSort(true).setCount(ListCount.MAX));
		result.addCol(toIntegerColumn("最大", "dbMaxLinkNum",76).setGroupTitle("DB链接").setSort(true).setCount(ListCount.MAX));
		result.addCol(toSpaceColumn("操作",
			ListButton.modal("服务请求", ButtonType.LINK,actionUrl("serverRequestList"),1200)
				.addParam("params[serverHost.oid]","oid")
		).setFixed(Align.RIGHT));
		result.addButton(ListButton.modal("汇总", ButtonType.PRIMARY, actionUrl("serverHostCollect"), 1000));
		result.addButton(ListButton.modal("监控设置",ButtonType.PRIMARY,actionUrl(ServerSettingAction.class,"toEdit"),800));
		result.addButton(ListButton.ajax("删除全部",ButtonType.ERROR,actionUrl(ServerSettingAction.class,"doDeleteAll"), SuccessMethod.REFRESH)
			.setConfirm("确定要删除全部监控记录吗?"));
		result.addBottomButton(ListButton.modal("全部SQL",ButtonType.DEFAULT,actionUrl("serverDetailSqlList"),1200));
//		result.addQueryFields("totalMemory","freeMemory","maxMemory","dbUseLinkNum","dbMaxLinkNum","memUsed","memTotal","cpuUser","cpuCombined");
		result.addSearch(ListSearch.field("JVM空闲内存","freeMemory",SearchType.NUMBER_RANGE));
		result.addSearch(ListSearch.field("JVM分配内存","totalMemory",SearchType.NUMBER_RANGE));
		result.addSearch(ListSearch.field("JVM最大内存","maxMemory",SearchType.NUMBER_RANGE));
		result.addSearch(ListSearch.field("已用DB链接","dbUseLinkNum",SearchType.NUMBER_RANGE));
		result.addSearch(ListSearch.field("最大DB链接","dbMaxLinkNum",SearchType.NUMBER_RANGE));
		result.setDefaultSearchNum(3);
		return result;
	}
	public VueResult serverRequestList() throws Exception {
		verifyMenu("A1108");
		VueListResult result=getVueListResult("服务请求",ServerRequest.class,actionUrl("serverRequestList"));
		result.setSubTitle("");
		result.setHeight(-30);
		result.addCol(toIndexColumn());
		result.addCol(toTextColumn("请求路径", "contextPath",300,true));
		result.addCol(toTextColumn("类型", "type",true));
		result.addCol(toTextColumn("后缀", "suffix",true));
		result.addCol(toNumberColumn("请求次数", "total",110,true).setSort(true).setNumberFormat("#,##0").setCount(ListCount.SUM));
		result.addCol(toNumberColumn("访问时长", "accessLong",110,true).setSort(true).setNumberFormat("#,##0.000").setCount(ListCount.SUM));
		result.addCol(toNumberColumn("平均时长", "avgLong",110,true).setSort(ListSortOrder.DESC).setNumberFormat("#,##0.000").setCount(ListCount.MAX));
		result.addCol(toIntegerColumn("记录SQL", "recordSql",110,true).setSort(true).setCount(ListCount.SUM));
		result.addCol(toSpaceColumn("操作",
			ListButton.modal("明细",ButtonType.LINK,actionUrl("serverRequestDetailList"),900)
				.addParam("params[serverRequest.oid]","oid"),
			ListButton.modal("SQL",ButtonType.LINK,actionUrl("serverDetailSqlList"),1200)
				.addParam("params[serverDetail.serverRequest.oid]","oid")
		).setFixed(Align.RIGHT));
		result.addSearch(ListSearch.field("","serverHost.oid",SearchType.HIDDEN_EQ));
		return result;
	}
	public VueResult serverRequestDetailList() throws Exception {
		verifyMenu("A1108");
		VueListResult result=getVueListResult("请求明细",ServerRequestDetail.class,actionUrl("serverRequestDetailList"));
		result.setSubTitle("");
		result.addCol(toIndexColumn());
		result.addCol(toTextColumn("访问ip", "logIp",true));
		result.addCol(toDateTimeColumn("记录时间", "logDate",true).setSort(ListSortOrder.DESC));
		result.addCol(toNumberColumn("访问时长", "accessLong",true).setNumberFormat("#,##0").setCount(ListCount.SUM));
		result.addCol(toTextColumn("User-Agent", "userAgent",300,true));
		result.addSearch(ListSearch.field("","serverRequest.oid",SearchType.HIDDEN_EQ));
		return result;
	}
	public VueResult serverDetailSqlList() throws Exception {
		VueListResult result=getVueListResult("请求SQL执行列表", ServerDetailSql.class,actionUrl("serverDetailSqlList"));
		result.setSubTitle("列表仅记录符合监控设置里的SQL语句。");
		result.setHeight(-1);
		result.setMergeCellsInfo(1,3);
		result.addCol(toIndexColumn());
		result.addCol(toTextColumn("请求路径", "serverDetail.serverRequest.contextPath", 110, true).setLineClamp(5));
		result.addCol(toDateTimeColumn("记录时间", "serverDetail.logDate", 95, true).setLineClamp(5));
		result.addCol(toTextColumn("记录ip", "serverDetail.logIp", 100, true));
		result.addCol(toIntegerColumn("执行时长", "exeLong", 100, true));
		result.addCol(toElementColumn("执行方法", "exeMethod", 100,
			ListElement.tag("{{record.exeMethod}}",TagColor.Processing.toString())
		).setAlign(Align.CENTER).setFilter(true).setDisItems(DisplayItem.to("Insert","Insert","Update","Update","BatchUpdate","BatchUpdate","BatchSql","BatchSql","Query","Query","First","First")));
		result.addCol(toElementColumn("执行结果", "exeResult", 100,
			new ListElement("a-tag").addText("{{record.exeResult}}").addAttr(":color","decode(record.exeResult,'Success','"+ TagColor.Success +"','"+TagColor.Warning+"')")
		).setAlign(Align.CENTER).setFilter(true).setDisItems(DisplayItem.to("Success","Success","Error","Error")));
		result.addCol(toTextColumn("执行sql", "exeSql", 200, true));
		result.addCol(toTextColumn("执行参数", "exeParam", 200, true));
		result.addCol(toSpaceColumn("操作", 80,
			ListButton.modal("查看", ButtonType.LINK,"page/base/host/viewSql.js",1000, SuccessMethod.REFRESH)
				.addParam("sql","exeSql").addParam("params","exeParam").addParam("method","exeMethod").addParam("result","exeResult")
		).setFixed(Align.RIGHT));
		result.addSearch(ListSearch.field("","serverDetail.serverRequest.oid",SearchType.HIDDEN_EQ));
		result.addOrder(
			QueryOrder.desc("serverDetail.logDate"),
			QueryOrder.asc("serverDetail.logIp")
		);
		//排序 result.addOrder
		//查询条件 result.addCondition
		return result;
	}
	public VueResult serverHostCollect() throws Exception {
		verifyMenu("A1108");
		FromQueryList from=FromQueryList.fromGroup("a",ServerHost.class);
		from.addOrder("DATE_FORMAT(#{a.logDate},'%Y') asc");
		VueCollectResult result=getVueCollectResult("主机汇总", from, actionUrl("serverHostCollect"),true);
		result.setFunction(true,true);
		result.setMergeCellsInfo(0, 2);
		result.addCol(collectTextColumn("年","DATE_FORMAT(#{a.logDate},'%Y') logYear",100,true));
		result.addCol(collectTextColumn("月","DATE_FORMAT(#{a.logDate},'%m') logMonth",100,true).setSort(ListSortOrder.ASC).setSubtotal(true));
		result.addCol(collectTextColumn("ipport","substring_index(#{a.ipport},':',1) ipport",100,true).setSort(true).setSubtotal(true));
		result.addCol(collectNumberColumn("访问量","sum(#{a.total}) total",100,true).setNumberFormat("#,##0").setGroupTitle("数字"));
		result.addCol(collectNumberColumn("时长","sum(#{a.accessLong}) accessLong",100,true).setGroupTitle("数字"));

		return result;
	}
}
